package com.intesmart.intesmartapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.intesmart.intesmartapp.hive.MainActivity2;

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private EditText fwq_ip;
    private EditText meetroom_name;
    private EditText meetroom_id;
    private EditText seat_num;
    private Button guanggaoji;
    private Button toupingji;
    private Button menpai;
    private Button menpai2;
    private Button zuopai;
    private Button toupiaoji;
    private Button fuwutai;
    private Button zhuchiji;
    private Button hujiaofuwu;
    private Button showSettingBtn;
    private Button showUserPanelBtn;
    private Button refreshWebBtn;
    private int longClickCount;

    private LinearLayout settingPanel;
    private LinearLayout userPanel;
    private String opurl;
    private String httpStr;
    private boolean longPressed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
//隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏状态栏
//定义全屏参数
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        longClickCount = 3;
        //方式二：这句代码必须写在setContentView()方法的前面
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

        setContentView(R.layout.activity_main);
        //仅去掉标题栏，系统状态栏还是会显示
        if (getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        webView = findViewById(R.id.webview);
        fwq_ip = findViewById(R.id.fwq_ip);
        meetroom_name = findViewById(R.id.meetroom_name);
        meetroom_id = findViewById(R.id.meetroom_id);
        seat_num = findViewById(R.id.seat_num);

        guanggaoji = findViewById(R.id.guanggaoji);
        toupingji = findViewById(R.id.toupingji);
        hujiaofuwu = findViewById(R.id.hujiaofuwu);
        zuopai = findViewById(R.id.zuopai);
        menpai = findViewById(R.id.menpai);
        menpai2 = findViewById(R.id.menpai2);
        zhuchiji = findViewById(R.id.zhuchiji);
        toupiaoji = findViewById(R.id.toupiaoji);
        fuwutai = findViewById(R.id.fuwutai);
        showSettingBtn = findViewById(R.id.showSettingBtn);
        showUserPanelBtn = findViewById(R.id.showUserPanelBtn);
        refreshWebBtn = findViewById(R.id.refreshWebBtn);

        settingPanel = findViewById(R.id.settingPanel);
        userPanel = findViewById(R.id.allPanel);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setSavePassword(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setDisplayZoomControls(false);

        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setHorizontalScrollbarOverlay(true);
        webView.setHorizontalScrollBarEnabled(true);
        webView.requestFocus();
        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示


        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
            //证书的设置  支持所有的证书
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                // 断网或者网络连接超时
                if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {
                    view.reload();
//                    view.loadUrl("about:blank"); // 避免出现默认的错误界面
//                    view.loadUrl(mErrorUrl);
                }
            }

            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                int errorCode = 0;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    errorCode = error.getErrorCode();
                }else{
                    view.reload();
                }
                // 断网或者网络连接超时
                if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {
                    view.reload();
//                    view.loadUrl("about:blank"); // 避免出现默认的错误界面
//                    view.loadUrl(mErrorUrl);
                }
            }

            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                throw new RuntimeException("Stub!");
            }

        });

        final HideControl hideControl = new HideControl();
        hideControl.startHideTimer();

        httpStr="http://";
        SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        fwq_ip.setText( sp.getString("fwq_ip" , "192.168.1.") );
        meetroom_id.setText( sp.getString("meetroom_id" , "1") );
        seat_num.setText( sp.getString("seat_num" , "") );
        meetroom_name.setText( sp.getString("meetroom_name" , "") );

        String purl = sp.getString("purl" , ""); // "/admin/fenping/zuopai"
        opurl = purl +"?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
        String url = httpStr + fwq_ip.getText().toString()  + opurl ;
        if (savedInstanceState == null)
        {
            webView.loadUrl( url );
        }


        guanggaoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/guanggaoji?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr +  fwq_ip.getText().toString()  + opurl + meetroom_name.getText().toString();
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/guanggaoji" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);

                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity( intent );
            }
        });
        zuopai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/zuopai?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString()  + opurl ;
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/zuopai" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
            }
        });

        View.OnClickListener menpaiListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/menpai?meetroom_id=" + meetroom_id.getText().toString() + "&seat_num=" + seat_num.getText().toString() + "&meetroom_name=" + meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString() + opurl;
                webView.loadUrl(url);
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip", fwq_ip.getText().toString());
                editor.putString("meetroom_name", meetroom_name.getText().toString());
                editor.putString("meetroom_id", meetroom_id.getText().toString());
                editor.putString("seat_num", seat_num.getText().toString());
                editor.putString("purl", "/admin/fenping/menpai");
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
            }
        };
        menpai.setOnClickListener(menpaiListener);
        menpai2.setOnClickListener(menpaiListener);
        zhuchiji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/zhuchiji?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString()  + opurl ;
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/zhuchiji" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
                settingPanel.setVisibility(View.GONE);
            }
        });
        toupiaoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/toupiaoji?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString()  + opurl ;
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/toupiaoji" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
                settingPanel.setVisibility(View.GONE);
            }
        });
        fuwutai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/fuwutai?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString()  + opurl ;
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/fuwutai" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
                settingPanel.setVisibility(View.GONE);
            }
        });
        hujiaofuwu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/hujiaofuwu?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString()  + opurl ;
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/hujiaofuwu" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
            }
        });
        toupingji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                opurl = "/admin/fenping/toupingji?meetroom_id="+meetroom_id.getText().toString()+"&seat_num="+seat_num.getText().toString()+"&meetroom_name=" +meetroom_name.getText().toString();
                String url = httpStr + fwq_ip.getText().toString()  + opurl ;
                webView.loadUrl( url );
                SharedPreferences sp = getSharedPreferences("perference", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("fwq_ip",  fwq_ip.getText().toString() );
                editor.putString("meetroom_name",  meetroom_name.getText().toString() );
                editor.putString("meetroom_id",  meetroom_id.getText().toString() );
                editor.putString("seat_num",  seat_num.getText().toString() );
                editor.putString("purl",  "/admin/fenping/toupingji" );
                editor.apply();
                hideControl.resetHideTimer();
                userPanel.setVisibility(View.GONE);
            }
        });

        refreshWebBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.reload();
            }
        });
        refreshWebBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //比如打开网络设置页面
                Intent intent=null;
                //判断手机系统的版本  即API大于10 就是3.0或以上版本
                if(android.os.Build.VERSION.SDK_INT > 10){
                    intent = new    Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                }else{
                    intent = new Intent();
                    ComponentName component = new ComponentName("com.android.settings","com.android.settings.ManageApplications");
                    intent.setComponent(component);          intent.setAction("android.intent.action.VIEW");
                }
                startActivity(intent);
                return true;
            }
        });
        showUserPanelBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                longClickCount += 1;
                if (userPanel.getVisibility() == View.GONE) {
                    userPanel.setVisibility(View.VISIBLE);
                }else{
                    userPanel.setVisibility(View.GONE);
                }
                if(longClickCount<3) {
                    userPanel.setVisibility(View.GONE);
                }
                return true;
            }
        });
        showSettingBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                longClickCount += 1;
                if (settingPanel.getVisibility() == View.GONE) {
                    settingPanel.setVisibility(View.VISIBLE);
                } else {
                    settingPanel.setVisibility(View.GONE);
                }
                if(longClickCount<3) {
                    settingPanel.setVisibility(View.GONE);
                }
                return true;
            }
        });
        showSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    public class HideControl {
        public final static int MSG_HIDE = 0x01;

        private HideHandler mHideHandler;

        public HideControl() {
            mHideHandler = new HideHandler();
        }

        public class HideHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG_HIDE:
                        userPanel.setVisibility(View.GONE);
                        showSettingBtn.setVisibility(View.VISIBLE);
                        break;
                }
            }
        }

        private Runnable hideRunable = new Runnable() {

            @Override
            public void run() {
                mHideHandler.obtainMessage(MSG_HIDE).sendToTarget();
            }
        };

        public void startHideTimer() {//开始计时,x秒后执行runable
            mHideHandler.removeCallbacks(hideRunable);
            int hide_time = 3;
            mHideHandler.postDelayed(hideRunable, 1000*60 * hide_time);
        }

        public void endHideTimer() {//移除runable,将不再计时
            mHideHandler.removeCallbacks(hideRunable);
        }

        public void resetHideTimer() {//重置计时
            mHideHandler.removeCallbacks(hideRunable);
            mHideHandler.postDelayed(hideRunable, 1000*60*1);
        }

    }
}
